from Tkinter import *
import WvongAlgorithm.globalSettings as globalSettings

def updateTesterSettings():
    
    testerModeVal = testerMode.get()
    fromIutEntryVal = entry_fromIut.get() 
    if fromIutEntryVal != '' and fromIutEntryVal != None:
        fromIutEntryInt = int(fromIutEntryVal)
        
    toIutEntryVal = entry_toIut.get()
    if toIutEntryVal != '' and toIutEntryVal != None:
        toIutEntryInt = int(toIutEntryVal)
        
    testerPrecisionEntryVal = entry_testerPrecision.get()
    if testerPrecisionEntryVal != '' and testerPrecisionEntryVal != None:
        testerPrecisionEntryInt = int(testerPrecisionEntryVal)
    
    
    testerPrecision = testerPrecisionEntryInt
    if testerModeVal == 1:
        fromIutDelay = fromIutEntryInt + toIutEntryInt
        toIutDelay = fromIutEntryInt + toIutEntryInt
    elif testerModeVal == 2:
        fromIutDelay = fromIutEntryInt + toIutEntryInt
        toIutDelay = fromIutEntryInt
    elif testerModeVal == 3:
        fromIutDelay = fromIutEntryInt + 2 * toIutEntryInt
        toIutDelay = fromIutEntryInt + toIutEntryInt
        
    print fromIutDelay, toIutDelay, testerPrecision
    label2_fromIutDelta.config(text = str(fromIutDelay))
    label2_toIutDelta.config(text = str(toIutDelay))
    label2_testerPrecision.config(text = str(testerPrecision))
    
    

root = Tk()
testerMode = IntVar()


fromIutDelay = 0
toIutDelay = 0
testerPrecision = 0

testMode = IntVar()

testerModeFrame = LabelFrame(root, text="Tryb pracy testera:")
testerModeFrame.pack(fill="both", expand="yes")

R1 = Radiobutton(testerModeFrame, text="tester \"przed\" IUT", variable=testerMode, value=1,
                  command=updateTesterSettings)
R1.pack( anchor = W )

R2 = Radiobutton(testerModeFrame, text="tester \"za\" IUT", variable=testerMode, value=2,
command=updateTesterSettings)
R2.pack( anchor = W )

R3 = Radiobutton(testerModeFrame, text="brak wiedzy o pozycji testera", variable=testerMode, value=3,
                  command=updateTesterSettings)
R3.pack( anchor = W)

otherSettingsFrame = LabelFrame(root, text="Inne ustawienia:")
otherSettingsFrame.pack(fill="both", expand="yes")

label_messageDelay = Label(otherSettingsFrame, text="Opoznienie dla wiadomosci:")
label_messageDelay.grid(row=0, column=0)

label_fromIut = Label(otherSettingsFrame, text="od IUT [ms]: ")
label_fromIut.grid(row=1, column=0)

entry_fromIut = Entry(otherSettingsFrame)
entry_fromIut.grid(row=1, column=1)

label_toIut = Label(otherSettingsFrame, text="do IUT [ms]: ")
label_toIut.grid(row=2, column=0)

entry_toIut = Entry(otherSettingsFrame)
entry_toIut.grid(row=2, column=1)

label_testerPrecision = Label(otherSettingsFrame, text="precyzja testera [ms]: ")
label_testerPrecision.grid(row=3, column=0)

entry_testerPrecision = Entry(otherSettingsFrame)
entry_testerPrecision.grid(row=3, column=1)

button_updateSettings = Button(otherSettingsFrame, text ="Uaktualnij parametry", command = updateTesterSettings)
button_updateSettings.grid(row=4, column=1)


testerParametersFrame = LabelFrame(root, text="Parametry testera:")
testerParametersFrame.pack(fill="both", expand="yes")

label1_fromIutDelta = Label(testerParametersFrame, text="Czas trzymania wiadomosci p w kolejce [ms]: ")
label1_fromIutDelta.grid(row=0, column=0)

label2_fromIutDelta = Label(testerParametersFrame)
label2_fromIutDelta.grid(row=0, column=1)

label1_toIutDelta = Label(testerParametersFrame, text="Czas trzymania wiadomosci q w kolejce [ms]: ")
label1_toIutDelta.grid(row=1, column=0)

label2_toIutDelta = Label(testerParametersFrame)
label2_toIutDelta.grid(row=1, column=1)

label1_testerPrecision = Label(testerParametersFrame, text="Precyzja testera [ms]: ")
label1_testerPrecision.grid(row=2, column=0)

label2_testerPrecision = Label(testerParametersFrame)
label2_testerPrecision.grid(row=2, column=1)



testModeFrame = LabelFrame(root, text="Rodzaj testu:")
testModeFrame.pack(fill="both", expand="yes")

radioButton_testFromFile = Radiobutton(testModeFrame, text="z pliku", variable=testMode, value=1)
radioButton_testFromFile.pack(anchor=W)

radioButton_testRandom = Radiobutton(testModeFrame, text="losowy", variable=testMode, value=2)
radioButton_testRandom.pack(anchor=W)

button_startTest = Button(testModeFrame, text ="Start testu", command = updateTesterSettings)
button_startTest.pack()


entry_fromIut.insert(0, str(globalSettings.FROM_IUT_DELTA))
entry_toIut.insert(0, str(globalSettings.TO_IUT_DELTA))
entry_testerPrecision.insert(0, str(globalSettings.TIME_QUANT))
R1.select()
updateTesterSettings()

root.mainloop()

